package com.yunhui.controller;



import com.yunhui.entity.Merchant;
import com.yunhui.entity.RouteIssued;

import com.yunhui.entity.RouteJb;
import com.yunhui.service.RouteJbService;
import com.yunhui.service.impl.RouteIssuedServiceImpl;
import com.yunhui.service.impl.RouteJbServiceImpl;

import com.yunhui.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  行程发布的前端控制器
 * </p>
 *
 * @author WGH
 * @since 2019-12-09
 */
@Controller
@RequestMapping("/routeIssued")
public class RouteIssuedController {
    @Autowired
    private RouteIssuedServiceImpl impl;

    @Autowired
    private RouteJbServiceImpl impljb;

    @RequestMapping("orderList")
    public String orderList(String pageIndex, String routeslssued, Model model, HttpSession session){
        Integer index = 1;// 默认页码为第一页

        //获取登录后的商家对象，在获取di
        Merchant m =  (Merchant)session.getAttribute("merchant");
        // 页码
        if (pageIndex != null && !"".equals(pageIndex)&&!"null".equals(pageIndex)) {
            index = Integer.valueOf(pageIndex);
        }

        //分页 第一查询总条数
        Integer totalCount = impl.findCount(routeslssued,m.getId());
        List<RouteIssued> routeIssuedList = impl.findPage(index,10,routeslssued,m.getId());
        model.addAttribute("routeIssued", routeIssuedList);
        model.addAttribute("routeIssuedname",routeslssued);
        System.err.println("routeIssuedList:" + routeIssuedList);

        PageSupport ps = new PageSupport();
        ps.setCurrentPageNo(index);
        ps.setPageSize(10);
        ps.setTotalCount(totalCount);

        Integer totalPageCount = ps.getTotalPageCount(); // 总页数

        model.addAttribute("page", ps);

        RouteJb r  = new RouteJb();
        r.setmId(m.getId());
        r.setStatus(2);
        List<RouteJb> routeJbList = impljb.getListRouteJb(r);
      // System.out.println("routeJbList:" + routeJbList);
        model.addAttribute("routeJbList" , routeJbList);

        System.err.println(totalCount);
        System.err.println(totalPageCount);
        System.err.println(index);


        return "shang/order-list";
    }

    /**
     * 删除
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteRoutlssuedAndJb")
    public Object deleteRoutlssuedAndJb(@RequestParam("idz")Integer id){
        impl.deleteRoutlssuedAndJb(id);
        System.err.println("id" + id);
        return id;
    }

    /**
     * 发布行程
     */
    @ResponseBody
    @RequestMapping("addRoutlssued")
    public String addRoutlssued(@RequestParam("jbid") Integer jbid, @RequestParam("date") Date startTimes,
                                @RequestParam("days") Integer days){

        System.err.println(startTimes);
       List<RouteIssued> list = impl.selectByJbIdandTime(jbid,startTimes);
        if(list.size()>0){
            return "-1";
        }
        int num =impl.addRouteIssued(jbid,startTimes,days);

        if(num>0){
            return "123";
        }
        return "-1";
    }

    /**
     * 根据行程id获取行程得发布列表
     * @param jbid
     * @return
     */
    @RequestMapping("selectByjbId")
    @ResponseBody
    public Object selectByjiId(@RequestParam("jbid") Integer jbid){
        List<RouteIssued>  list =  impl.selectByJbIdandTime(jbid,null);
        return  list;
    }

    @Resource
    private RouteJbService routeJbService;


    /**
     * 查询行程发布得人数
     * @param jbid
     * @param number
     * @return
     */
    @RequestMapping("selectPersonCount")
    @ResponseBody
    public Object selectPersonCount(Integer jbid,String number){

        //用户人数
        int num  = Integer.parseInt(number);
        RouteIssued rf= impl.getById(jbid);

        RouteJb r =  routeJbService.getById(rf.getJbId());

        //行程团的总人数
        int countNum = r.getMaxPerson();
        //行程团的现有人数
        int countFaCount = impl.selectByJbId(jbid);

        if ((num+countFaCount)<countNum){
            return "1";
        }
        return  "-1";
    }

}

